<?php

namespace app\business\controller;

use app\business\model\MediumModel;
use app\resource\model\CurrencyModel;
use cmf\controller\AdminBaseController;

class MediumController extends AdminBaseController
{
    /**
     * 中介列表
     * @adminMenu(
     *     'name'   => '中介列表',
     *     'parent' => 'business/LicensePlate/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 100,
     *     'icon'   => '',
     *     'remark' => '中介列表',
     *     'param'  => ''
     * )
     */
    function index()
    {
        $mediumModel = new MediumModel();

        $where = [];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['medium.uid'] = ['in', $users];
        }

        if (input('keyword')) {
            $keyword = input('keyword');
            $where['medium.company|medium.name|medium.phone|medium.address|medium.business|medium.management_model|user.user_nickname'] = ['like', "%$keyword%"];
        }

        $lists = $mediumModel
            ->alias('medium')
            ->join('__USER__ user', 'medium.uid = user.id')
            ->field('medium.*,user.user_nickname')
            ->where($where)
            ->order('id desc')
            ->paginate();

        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $this->assign('where_r', $where_r);
        $this->assign('lists', $lists);
        $this->assign('page', $page);
        return $this->fetch();
    }

    /**
     * 添加
     * @adminMenu(
     *     'name'   => '添加',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加',
     *     'param'  => ''
     * )
     */
    function add()
    {
        if ($this->request->isPost()) {
            $arrData = $this->request->post();

            $mediumModel = new MediumModel();
            $result = $mediumModel->allowField(true)->validate(true)->save($arrData);
            if ($result) {
                $this->success('添加成功', url('index'));
            } else {
                $this->error($mediumModel->getError());
            }
        }
        $currency = new CurrencyModel();
        $defCurrency = $currency->where(['id' => 1])->find();

        $this->assign('defCurrency', $defCurrency);
        return $this->fetch();
    }

    /**
     * 编辑
     * @adminMenu(
     *     'name'   => '编辑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑',
     *     'param'  => ''
     * )
     */
    function edit()
    {
        if ($this->request->isPost()) {
            $arrData = $this->request->post();
            if (empty($arrData['id'])) {
                $this->error('参数错误');
            }

            $mediumModel = new MediumModel();
            $result = $mediumModel->allowField(true)->validate(true)->save($arrData, [
                'id' => $arrData['id']
            ]);
            if ($result) {
                $this->success('保存成功', url('index'));
            } else {
                $this->error($mediumModel->getError());
            }
        }
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            $this->error('参数错误');
        }
        $mediumModel = new MediumModel();
        $result = $mediumModel
            ->alias('medium')
            ->join('__CURRENCY__ currency', 'medium.currency = currency.id', 'left')
            ->join('__CUSTOMER_NATURE__ snature', 'medium.staffs = snature.id', 'left')
            ->join('__CUSTOMER_NATURE__ tnature', 'medium.turnover = tnature.id', 'left')
            ->field('medium.*,currency.name cname,snature.name sname,tnature.name tname')
            ->find($id);

        $this->assign('result', $result);
        return $this->fetch();
    }
}